package org.example.slave.fetch;

import cn.hutool.core.map.MapUtil;
import cn.hutool.extra.spring.SpringUtil;
import org.example.slave.enums.SourceType;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;

/**
 * @author dingrui
 * @since 2022/2/14
 * @description
 */
@Component
public class FetcherFactory {

    // 根据数据源类型创建对应的实例
    public Fetcher create(SourceType sourceType) {
        if (Objects.isNull(sourceType)) return null;
        Map<String, Fetcher> beans = SpringUtil.getBeansOfType(Fetcher.class);
        if (MapUtil.isEmpty(beans)) return null;
        return beans.values().stream().filter(o -> Objects.equals(sourceType, o.strategyType())).findFirst().orElseGet(() -> null);
    }
}
